package system

import (
	"github.com/beego/beego/v2/client/orm"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

// 菜单表
type SysMenu struct {
	MenuId       int64     `orm:"column(menu_id);pk"`
	ParentId     int64     `description:"父级菜单ID"`
	RootId       int64     `description:"顶层菜单ID"`
	Name         string    `description:"菜单名称或者按钮名称"`
	Sort         int8      `description:"排序序号"`
	Path         string    `description:"路由地址"`
	PathName     string    `description:"路由名称"`
	IconPath     string    `description:"菜单图标地址"`
	Component    string    `description:"组件路径"`
	Hidden       int8      `description:"是否隐藏：1-隐藏 0-显示"`
	HiddenLayout int8      `description:"是否隐藏layout：1-隐藏 0-显示"`
	Level        int8      `description:"层级"`
	MenuType     int8      `description:"菜单类型： 0-菜单， 1-按钮"`
	ButtonCode   string    `description:"按钮唯一标识"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

type SysMenuAddReq struct {
	ParentId     int64  `description:"父级菜单ID"`
	Name         string `description:"菜单名称或者按钮名称"`
	Sort         int8   `description:"排序序号"`
	Path         string `description:"路由地址"`
	PathName     string `description:"路由名称"`
	IconPath     string `description:"菜单图标地址"`
	Component    string `description:"组件路径"`
	Hidden       int8   `description:"是否隐藏：1-隐藏 0-显示"`
	HiddenLayout int8   `description:"是否隐藏layout：1-隐藏 0-显示"`
	MenuType     int8   `description:"菜单类型： 0-菜单， 1-按钮"`
	ButtonCode   string `description:"按钮唯一标识"`
}
type SysMenuEditReq struct {
	MenuId int64 `description:"菜单ID"`
	SysMenuAddReq
}

// 新增
func (m *SysMenu) Add() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(m)
	return
}

// 修改
func (m *SysMenu) Update(updateParams, whereParam map[string]interface{}) (err error) {
	to := orm.NewOrm()
	ptrStructOrTableName := "sys_menu"

	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

func (r *SysMenu) GetSysMenuPageCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM sys_menu WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *SysMenu) GetSysMenuPage(condition string, pars []interface{}, startSize, pageSize int) (items []*SysMenu, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_menu WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type SysMenuButtonResp struct {
	MenuId     int64  `description:"菜单ID"`
	ParentId   int64  `description:"父级菜单ID"`
	Name       string `description:"菜单名称或者按钮名称"`
	MenuType   int8   `description:"菜单类型： 0-菜单， 1-按钮"`
	ButtonCode string `description:"按钮唯一标识"`
}

func (m *SysMenu) GetMenuListByCondition(condition string, pars []interface{}, orderStr string) (results []*SysMenu, err error) {
	results = make([]*SysMenu, 0)
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_menu WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	if orderStr == "" {
		sql += ` ORDER BY sort ASC, create_time DESC`
	} else {
		sql += ` ORDER BY ` + orderStr
	}
	_, err = o.Raw(sql, pars...).QueryRows(&results)
	return

}

type MenuShortListItem struct {
	MenuId int64 `description:"菜单ID"`
}

type MenuListReq struct {
	utils.PagingReq
	Name       string `description:"菜单名称或者按钮名称"`
	HideLevel3 int8   `description:"返回值是否需要隐藏三级的菜单和三级按钮（1-隐藏，0-不隐藏）"`
	HideButton int8   `description:"返回值是否需要隐藏按钮（1-隐藏，0-不隐藏）"`
}

type MenuListItemResp struct {
	MenuId       int64  `description:"菜单ID"`
	ParentId     int64  `description:"父级菜单ID"`
	Name         string `description:"菜单名称或者按钮名称"`
	Sort         int8   `description:"排序序号"`
	Path         string `description:"页面地址或者Api请求地址"`
	PathName     string `description:"路由名称"`
	IconPath     string `description:"菜单图标地址"`
	Component    string `description:"组件路径"`
	Hidden       int8   `description:"是否隐藏：1-隐藏 0-显示"`
	HiddenLayout int8   `description:"是否隐藏layout：1-隐藏 0-显示"`
	Level        int8   `description:"成绩"`
	MenuType     int8   `description:"菜单类型： 0-菜单， 1-按钮"`
	ButtonCode   string `description:"按钮唯一标识"`
	HasBind      bool   `description:"是否已绑定，true 已绑定，false 未绑定"`
	CreateTime   string `description:"创建时间"`
	ModifyTime   string `description:"最后更新时间"`
	Children     []*MenuListItemResp
}

func (m *SysMenu) GetMenuByName(name string) (list []*SysMenu, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_menu WHERE name LIKE '%` + name + `%'`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

func (m *SysMenu) GetMenuByMenuId(menuId int64) (item *SysMenu, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_menu WHERE menu_id = ?`
	err = o.Raw(sql, menuId).QueryRow(&item)
	return
}

// 删除
func (m *SysMenu) DeleteByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM sys_menu WHERE 1=1 `
	sql += condition
	_, err = o.Raw(sql, pars...).Exec()
	return

	return
}

type SysMenuAllListPageResp struct {
	List   []*MenuListItemResp
	Paging *utils.PagingResp
}
